package com.lovo.pz.role;

import com.lovo.pz.util.GameImage;
import com.lovo.pz.util.GameUtil;
import com.lovo.pz.util.UserBean;
import com.lovo.pz.window.GamePanel;

import java.awt.*;

import java.util.List;

/**
 * 僵尸角色
 * @author 白德柯
 *
 */
public class ZombieRole extends BaseRole {
    /** 僵尸行走状态 */
    public static final int ZOMBIE_WALK = 1;
    /** 僵尸攻击状态 */
    public static final int ZOMBIE_ATTACK = 2;
    /** 僵尸头掉落状态 */
    public static final int ZOMBIE_LOST_HEAD = 3;
    /** 无头僵尸行走状态 */
    public static final int ZOMBIE_NOHEAD_WALK = 4;
    /** 无头僵尸攻击状态 */
    public static final int ZOMBIE_NOHEAD_ATTACK = 5;
    /** 僵尸死亡状态 */
    public static final int ZOMBIE_DEAD = 6;
    /** 无头僵尸状态 */
    public static final int ZOMBIE_NO_HEAD = 7;
    /** 带桶僵尸行走状态 */
    public static final int BUCKETHEAD_ZOMBIE_WALK = 8;
    /** 带桶僵尸攻击状态 */
    public static final int BUCKETHEAD_ZOMBIE_ATTACK = 9;
    /** 僵尸的分数 */
    private int score;
    /** 僵尸的状态 */
    private int zombieType;
    /** 无头僵尸的状态 */
    private int lostHeadType;

    private Image zombieHeadImage;
    private Image zombieDeadImage;
    public ZombieRole(Component component){
        super(component);
    }
    @Override
    public void initRole() {
        // 每个僵尸角色都重新加载一次头的图片
        zombieHeadImage = GameImage.loadImage(GameUtil.zombieHeadImage);
        // 每个僵尸角色都重新加载一次死掉的图片
        zombieDeadImage = GameImage.loadImage(GameUtil.zombieDeadImage);
        int posX = (int)(Math.random() * 150);
        int posY = (int)(Math.random() * 5);
        setRoleX(posX + 1000);
        setRoleY(posY * 92 + 110);

        setRoleWidth(50);
        setRoleHeight(50);
        setMoveLength(20);
        // 初始化普通僵尸的走路状态
        zombieType = ZOMBIE_WALK;
        // 设置普通僵尸的HP值
        setHp(20);
        setScore(100);
    }

    @Override
    public void paintRole(Graphics g) {
        if(getZombieType() == ZOMBIE_WALK){
            g.drawImage(GameUtil.zombieWalkImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
        if(getZombieType() == ZOMBIE_ATTACK){
            g.drawImage(GameUtil.zombieAttackImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
        if(getZombieType() == ZOMBIE_NOHEAD_WALK){
            g.drawImage(GameUtil.zombieLostHeadWalkImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
        if(getZombieType() == ZOMBIE_NOHEAD_ATTACK){
            g.drawImage(GameUtil.zombieLostHeadAttackImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
        if(getZombieType() == ZOMBIE_DEAD){
            g.drawImage(GameUtil.zombieDeadImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
        if(getZombieType() == ZOMBIE_LOST_HEAD){
            g.drawImage(GameUtil.zombieHeadImage,getRoleX() - 80,getRoleY() - 70,getComponent());
        }
    }

    /**
     * 检测僵尸和植物的碰撞
     * @return
     */
    public boolean checkZombieAndPlantsImpact(){
        GamePanel gamePanel = (GamePanel) getComponent();
        List<PlantRole> plantRoleList = gamePanel.getPlantRoleList();
        for(int i = 0;i < plantRoleList.size();i++){
            if(GameUtil.checkImpact(this,plantRoleList.get(i))){
                plantRoleList.get(i).setHp(plantRoleList.get(i).getHp() - 1);
                return true;
            }
        }
        return false;
    }
    @Override
    public void action() {
        GamePanel gamePanel = (GamePanel) getComponent();
        if(getHp() < 10){
            if(lostHeadType != ZOMBIE_NO_HEAD){
                lostHeadType = ZOMBIE_LOST_HEAD;
            }
            if(GameUtil.delay(1000,this,"losthead")){
                lostHeadType = ZOMBIE_NO_HEAD;
            }
            setZombieType(ZOMBIE_NOHEAD_WALK);
        }
        if(getHp() <= 0){
            UserBean userBean = gamePanel.getMainFrame().getUserBean();
            userBean.setScore(userBean.getScore() + getScore());
            setScore(0);
            setZombieType(ZOMBIE_DEAD);
            if(GameUtil.delay(1000,this,"dead")){
                setRunStatus(ROLE_END);
            }
        }else{
            // 检测僵尸是否碰到了植物
            if(checkZombieAndPlantsImpact()){
                if(getZombieType() == ZOMBIE_WALK){
                    setZombieType(ZOMBIE_ATTACK);
                }else if(getZombieType() == ZOMBIE_NOHEAD_WALK){
                    setZombieType(ZOMBIE_NOHEAD_ATTACK);
                }else if(getZombieType() == BUCKETHEAD_ZOMBIE_WALK){
                    setZombieType(BUCKETHEAD_ZOMBIE_ATTACK);
                }
            }else{
                if(getZombieType() == ZOMBIE_ATTACK){
                    setZombieType(ZOMBIE_WALK);
                }else if(getZombieType() == ZOMBIE_NOHEAD_ATTACK){
                    setZombieType(ZOMBIE_NOHEAD_WALK);
                }else if(getZombieType() == BUCKETHEAD_ZOMBIE_ATTACK){
                    setZombieType(BUCKETHEAD_ZOMBIE_WALK);
                }
                if(GameUtil.delay(200,this,"zombieMove")){
                    setRoleX(getRoleX() - getMoveLength());
                }
            }
            if(getRoleX() < 0){
                // 触发游戏结束
                gamePanel.setGameStatus(GAME_ZOMBIE_WON);
            }
        }
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getZombieType() {
        return zombieType;
    }

    public void setZombieType(int zombieType) {
        this.zombieType = zombieType;
    }

    public int getLostHeadType() {
        return lostHeadType;
    }

    public void setLostHeadType(int lostHeadType) {
        this.lostHeadType = lostHeadType;
    }
}
